package com.zmj.framework.dao.jdbc;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.stereotype.Repository;

import com.zmj.framework.bean.Codetype;
import com.zmj.framework.dao.CodetypeDao;
import com.zmj.framework.util.GeneralRowMapper;
import com.zmj.framework.util.PageController;
@Repository
public class CodetypeDaoJdbc implements CodetypeDao {
	@Autowired
	FrmJdbcTemplate jdbcTemplate;

	public CodetypeDaoJdbc() {
		this.jdbcTemplate = null;
	}

	public List getCodetypes(Codetype codetype) throws SQLException {
		String tmpSql = " where dmlb<>'000001' ";

		if ((codetype.getDmlb() != null) && (!(codetype.getDmlb().equals("")))) {
			tmpSql = " and dmlb like '" + codetype.getDmlb() + "%'";
		}

		if ((codetype.getLbsm() != null) && (!(codetype.getLbsm().equals("")))) {
			tmpSql = tmpSql + " and lbsm like '%" + codetype.getLbsm() + "%'";
		}

		tmpSql = "select * from frm_codetype " + tmpSql + " order by dmlb";
		List list = this.jdbcTemplate.queryForList(tmpSql, Codetype.class);
		return list;
	}

	public Codetype getCodetype(String dmlb) throws SQLException {
		String tmpSql = "select * from frm_codetype where dmlb='" + dmlb + "'";
		List list = this.jdbcTemplate.queryForList(tmpSql, Codetype.class);

		if ((list == null) || (list.size() == 0)) {
			return null;
		}
		return ((Codetype) list.get(0));
	}

	public int saveCodetype(Codetype frmCodetype) throws SQLException {
		Connection conn = DataSourceUtils.getConnection(this.jdbcTemplate.getDataSource());

		CallableStatement cstmt = conn.prepareCall("{call FRM_SYS_PKG.saveFRM_CODETYPE(?,?,?,?,?,?,?)}");

		if (frmCodetype.getDmlb() == null) {
			cstmt.setNull(1, 12);
		} else {
			cstmt.setString(1, frmCodetype.getDmlb());
		}
		if (frmCodetype.getLbsm() == null) {
			cstmt.setNull(2, 12);
		} else {
			cstmt.setString(2, frmCodetype.getLbsm());
		}
		cstmt.setLong(3, frmCodetype.getDmcd());

		if (frmCodetype.getLbsx() == null) {
			cstmt.setNull(4, 12);
		} else {
			cstmt.setString(4, frmCodetype.getLbsx());
		}
		if (frmCodetype.getLbbz() == null) {
			cstmt.setNull(5, 12);
		} else {
			cstmt.setString(5, frmCodetype.getLbbz());
		}
		if (frmCodetype.getBz() == null) {
			cstmt.setNull(6, 12);
		} else {
			cstmt.setString(6, frmCodetype.getBz());
		}
		cstmt.registerOutParameter(7, 2);
		cstmt.execute();
		int iResult = cstmt.getInt(7);
		cstmt.close();

		DataSourceUtils.doReleaseConnection(conn, this.jdbcTemplate.getDataSource());
		return iResult;
	}

	public int removeCodetype(String dmlb) throws SQLException {
		Connection conn = DataSourceUtils.getConnection(this.jdbcTemplate.getDataSource());

		CallableStatement cstmt = conn.prepareCall("{call FRM_SYS_PKG.deleteFRM_CODETYPE(?,?)}");

		if (dmlb == null) {
			cstmt.setNull(1, 12);
		} else {
			cstmt.setString(1, dmlb);
		}
		cstmt.registerOutParameter(2, 2);
		cstmt.execute();
		int iResult = cstmt.getInt(2);
		cstmt.close();

		DataSourceUtils.doReleaseConnection(conn, this.jdbcTemplate.getDataSource());
		return iResult;
	}

	public List getCodetypesByPageSize(Codetype codetype, PageController controller) throws SQLException {
		String tmpSql = "";

		if ((codetype.getDmlb() != null) && (!(codetype.getDmlb().equals("")))) {
			tmpSql = " where dmlb like '" + codetype.getDmlb() + "%'";
		}

		if ((codetype.getLbsm() != null) && (!(codetype.getLbsm().equals("")))) {
			if (tmpSql != "") {
				tmpSql = tmpSql + " and lbsm like '%" + codetype.getLbsm() + "%'";
			} else {
				tmpSql = " where lbsm like '%" + codetype.getLbsm() + "%'";
			}
		}
		tmpSql = "select * from frm_codetype " + tmpSql + " order by dmlb";
		GeneralRowMapper mapper = new GeneralRowMapper(Codetype.class);
		return controller.getWarpedList(tmpSql, mapper, this.jdbcTemplate);
	}
}